package com.ruoyi.web.controller.device;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Energy;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.EnergyService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: ruohao
 * @Date: 2023/9/14
 **/
@RestController
@RequestMapping("/device/energy")
public class EnergyController extends BaseController {
    @Resource
    private EnergyService energyService;
    @Log(title = "查询人员管理", businessType = BusinessType.OTHER)
    @GetMapping("/list")
    public TableDataInfo list(Energy energy)
    {
        startPage();
        List<Energy> list = energyService.selectList(energy);
        return getDataTable(list);
    }

    @Log(title = "能耗导出", businessType = BusinessType.EXPORT)
    //("@ss.hasPermi('system:user:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Energy energy)
    {
        List<Energy> list = energyService.selectList(energy);
        ExcelUtil<Energy> util = new ExcelUtil<Energy>(Energy.class);
        util.exportExcel(response, list, "能耗数据");
    }

    @Log(title = "查询人员管理All", businessType = BusinessType.OTHER)
    @GetMapping("/listAll")
    public TableDataInfo listAll(Energy energy)
    {
        List<Energy> list = energyService.selectList(energy);
        return getDataTable(list);
    }
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success(energyService.selectById(id));
    }

    @Log(title = "新增人员管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody Energy energy)
    {
        energy.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = energyService.create(energy);
        if (result) {
            return success();
        }
        return error();
    }

    @Log(title = "修改人员管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Energy energy) {
        energy.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = energyService.update(energy);
        if (result) {
            return success();
        }
        return error();
    }

//    @Log(title = "删除人员管理", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{id}")
//    public AjaxResult remove(@PathVariable Long id)
//    {
//        return toAjax(energyService.deleteById(id));
//    }
    /**
     * 删除【请填写功能名称】
     */
    //("@ss.hasPermi('system:energy:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(energyService.deleteEnergyByIds(ids));
    }

    @Log(title = "查询人员管理", businessType = BusinessType.OTHER)
    @GetMapping("/fiveList")
    public AjaxResult list()
    {
        List<Energy> list = energyService.selectFiveList();
        return AjaxResult.success(list);
    }


}
